export default {
    data() {
        return {

            // 表单数据
            ruleForm: {
                account_number: '',
                paddword: '',
                name: '',
                phone_number: '',
                authority: ''
            },

            // 是否显示编辑按钮
            is_edit: true,

            // 表单验证
            ruleFormRules: {
                account_number: [{
                    required: true,
                    message: '请输入账号',
                    trigger: 'blur'
                }, ],
                paddword: [{
                    required: true,
                    message: '请输入密码',
                    trigger: 'blur'
                }, ],
                name: [{
                    required: true,
                    message: '请输入姓名',
                    trigger: 'blur'
                }, ],
                phone_number: [{
                    required: true,
                    message: '请输入电话号码',
                    trigger: 'blur'
                }, ],
                authority: [{
                    required: true,
                    message: '请选择权限',
                    change: 'blur'
                }, ]
            },
        }
    },

    //进入页面加载
    mounted: function () {
        var that = this;
        //在缓存中获取值
        that.user_name = sessionStorage.getItem("user_name");
        that.user_id = sessionStorage.getItem("user_id");
        that.token = sessionStorage.getItem("access-token");
        if ((!that.token) || (!that.user_id) || (!that.user_name)) {
            that.$router.push('/login');
        }

        // 获取单个员工数据
        if (that.$route.query.id) {
            that.detail(that.$route.query.id);

            // 编辑员工 则隐藏密码框
            that.is_edit = false;
        }

    },

    methods: {

        /**
         * 获取单个员工数据
         */
        detail(id) {
            var that = this;

            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.id = that.user_id;

            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/manager/index", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 获取的数据
                    var data = res.body.list;

                    // 处理成功的结果
                    if (data) {
                        for (var i in data.list) {
                            if (data.list[i].id == id) {
                                that.ruleForm.account_number = data.list[i].username;
                                that.ruleForm.name = data.list[i].username;
                                that.ruleForm.phone_number = data.list[i].mobile;

                                // 管理员
                                var role_num = data.list[i].role_name == '超级管理员' ? '1' : '2';

                                that.ruleForm.authority = role_num;
                            }
                        }
                    }
                });
        },

        /**
         * 保存员工数据
         */
        save: function (formName) {
            let that = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    //请求的数据
                    var formData = {};
                    formData.token = that.token;

                    formData.mobile = that.ruleForm.phone_number; //手机号
                    formData.username = that.ruleForm.name; //姓名
                    formData.identity = that.ruleForm.authority; //权限

                    if (that.is_edit) {
                        formData.password = that.ruleForm.paddword; //密码
                    }

                    that.$http.post(that.adminApi.api_url + "/manager/add", formData, {
                        emulateJSON: true
                    }).then(
                        function (res) {
                            var data = res.body;

                            if (data.status == 1) {
                                // 处理成功的结果
                                that.$message.success('添加成功');

                                // 清空表单数据
                                that.ruleForm.account_number = '';
                                that.ruleForm.paddword = '',
                                    that.ruleForm.name = '',
                                    that.ruleForm.phone_number = '',
                                    that.ruleForm.authority = ''
                            }

                        }).catch(function (res) {
                        var data = res.body;
                        that.$message.error(`${data.msg}`);
                    });
                } else {
                    // console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 返回
         */
        back: function () {
            this.$router.go(-1);
        }
    }
}